﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// FlightRead 的摘要说明
    /// </summary>
    public class FlightRead : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "text/plain";
                int pageNo = int.Parse(context.Request["page"]);
                string keywords = context.Request["key"];

                string sqllist = "select Top 10 FLIGHT_ID,FLIGHT_CODE,FLIGHT_CITY_START,FLIGHT_CITY_END,FLIGHT_TIME_START,FLIGHT_TIME_END,FLIGHT_PRICE from (select row_number() over(order by FLIGHT_CODE desc) as rownumber,* from FLIGHT {0}) A where rownumber > {1}";
                string sqlcount = "select count(*) from FLIGHT";
                if (!string.IsNullOrEmpty(keywords))
                {
                    string filter = string.Format(" where FLIGHT_CODE like '%{0}%'", keywords);
                    sqlcount += filter;
                    sqllist = string.Format(sqllist, filter, (pageNo - 1) * 10);
                }

                sqllist = string.Format(sqllist, keywords, (pageNo - 1) * 10);

                Flight_BLL bll = new Flight_BLL();
                int total = bll.GetCount(sqlcount);
                List<FLIGHT> flights = bll.GetModels(sqllist);
                Paginator<FLIGHT> paginator = new Paginator<FLIGHT>();
                paginator.total = total;
                paginator.list = flights;

                context.Response.Write(JsonConvert.SerializeObject(paginator));
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("读取航班失败", ex);
                context.Response.Write("{\"count\":0,\"list\":[]}");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}